package com.eventbus.demo.springevent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EventPublisherController {
    Logger logger = LoggerFactory.getLogger(EventPublisherController.class);
    /**
     * 注入spring 事件发布器
     */
    @Autowired
    private ApplicationEventPublisher eventPublisher;


    @RequestMapping("/publish")
    public String publish(String title,String content){
        NoticeEvent event = new NoticeEvent(title,content);
        logger.info("接口收到请求，内容如下：");
        logger.info("{}",event);
        eventPublisher.publishEvent(event);
        String ret = "事件发布成功";
        logger.info(ret);
        return ret;
    }
}
